package com.IT.dao;

import com.IT.pojo.User;
import com.IT.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//测试类
public class UserDaoText {
    @Test//查询所有
    public void test(){
        //1.获得sqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();;
        //2.执行sql
        try{
            sqlSession = MybatisUtils.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            List<User> userList = userMapper.getUserList();
            for (User user : userList) {
                System.out.println(user);
            }
//            User userById = userMapper.getUserById(1);
//            System.out.println(userById);
           }
        catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }


        //关闭sqlSession

    }
    @Test//根据ID查询
    public void getUserById(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserById(5);
        System.out.println(user);


        sqlSession.close();
    }
    //增删改必须要提交数据/事务
    @Test//添加
    public void addUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int ls = mapper.addUser(new User(4, "李丁", "1477"));
        if (ls!=0){
            System.out.println("执行完毕");
        }
        //提交事务
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void addUser2 (){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        Map<String, Object> map = new HashMap<String,Object>();
        map.put("userid",6);
        map.put("userName","丽丽");
        map.put("passWord","2222");
        mapper.addUser2(map);
        
        sqlSession.commit();
        sqlSession.close();
    }

    @Test//修改
    public void updateUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int ls = mapper.updateUser(new User(6,"刘飞","456"));
        if (ls!=0){
            System.out.println("执行完毕");
        }
        //提交事务
        sqlSession.commit();
        sqlSession.close();

    }
    @Test//删除
    public void deletUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int ls = mapper.deletUser(4);
        if (ls!=0){
            System.out.println("执行完毕");
        }
        //提交事务
        sqlSession.commit();
        sqlSession.close();

    }
    @Test
    public void getUserByLimit(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("startIndex",0);
        map.put("pageSize",2);
        List<User> userList = mapper.getUserByLimit(map);
        for (User user : userList) {
            System.out.println(user);
        }
        sqlSession.close();
    }

}
